package hk.m4s.lr.repair.test.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.NewsModel;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class NewsAdapter extends BaseAdapter {
    public List<NewsModel> list = new ArrayList<>();
    Context context;
    LayoutInflater inflater;

    public NewsAdapter(Context context, List<NewsModel> data) {
        this.context = context;
        this.list = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_news, parent, false);
            holder.news_title = (TextView) convertView.findViewById(R.id.news_title);
            holder.news_content = (TextView) convertView.findViewById(R.id.news_content);
            holder.news_times = (TextView) convertView.findViewById(R.id.news_times);
            holder.news_shares = (TextView) convertView.findViewById(R.id.news_shars);
            holder.news_looks = (TextView) convertView.findViewById(R.id.news_looks);
            holder.news_image = (ImageView) convertView.findViewById(R.id.news_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        NewsModel newsModel = list.get(position);
        holder.news_title.setText(newsModel.getName());
        holder.news_content.setText(newsModel.getSummary());
        holder.news_times.setText(newsModel.getSubtime() + "");
        holder.news_shares.setText(newsModel.getCount());
        holder.news_looks.setText(newsModel.getCount());
        try {
            Glide.with(context).load(newsModel.getLogo()).asBitmap().error(R.mipmap.sebei_logo).into(holder.news_image);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public static class ViewHolder {
        TextView news_title;
        TextView news_content;
        TextView news_times;
        TextView news_looks;
        TextView news_shares;
        ImageView news_image;
    }
}
